package com.woniuxy.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.crm.entity.Category;
import com.woniuxy.crm.result.Result;
import com.woniuxy.crm.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类型表 前端控制器
 *
 * @author 郑鹏
 * @since 2024-08-12
 */
@RestController
@RequestMapping("/admin/category")
@Tag(name = "类型表接口")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    // 新增或者更新
    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增或者修改")
    public Result save(@RequestBody Category category) {
        categoryService.saveOrUpdate(category);
        return Result.ok();
    }

    @DeleteMapping("/deleteById")
    @Operation(summary = "根据id删除")
    public Result delete(@RequestParam Long id) {
        categoryService.removeById(id);
        return Result.ok();
    }

//    @PostMapping("/del/batch")
//    @Operation(summary = "批量删除")
//    public boolean deleteBatch(@RequestBody List<Integer> ids) {
//        return categoryService.removeByIds(ids);
//    }

    @GetMapping("/list")
    @Operation(summary = "查询全部")
    public Result<List<Category>> findAll() {
        List<Category> list = categoryService.list();
        return Result.ok(list);
    }

    @GetMapping("/listById")
    @Operation(summary = "根据id查询")
    public Result<Category> findOne(@RequestParam Long id) {
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<Page<Category>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getId);
        Page<Category> page = categoryService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.ok(page);

    }

}

